import java.util.StringTokenizer;
import java.sql.*;

/**
 * Created by IntelliJ IDEA.
 * User: christopher_moore
 * Date: Jul 24, 2007
 * Time: 2:25:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class Auction {
    private String serverFaction;
    private String objectId;
    private int auctionId;
    private long uniqueId;
    private int count;
    private int minBid;
    private int buyoutPrice;
    private String owner;
    private int bidAmount;
    private boolean highBidder;
    private int timeLeft;
    private long lastSeen;
    private long expiration;

    public Auction(String dataString) {
        int delim1;
        int delim2;
        String auctionIdString;
        String rightHandSide;
        int itemId;
        int suffixId;
        int enchantId;

        if (dataString.startsWith("nil")) {
            // Don't know why these are in the auctioneer.lua file, but sometimes they are
            auctionId = -1;
            return;
        }

        delim1 = dataString.indexOf("[");
        delim2 = dataString.indexOf("]");
        auctionIdString = dataString.substring(delim1+1, delim2);
        this.auctionId = Integer.parseInt(auctionIdString);

        delim1 = dataString.indexOf("\"");
        delim2 = dataString.lastIndexOf("\"");
        try {
            rightHandSide = dataString.substring(delim1+1, delim2);


            StringTokenizer st = new StringTokenizer(rightHandSide, ";");
            itemId = Integer.parseInt(st.nextToken());
            suffixId = Integer.parseInt(st.nextToken());
            enchantId = Integer.parseInt(st.nextToken());
            this.objectId = itemId + ":" + suffixId + ":" + enchantId;
            this.uniqueId = Long.parseLong(st.nextToken());
            this.count = Integer.parseInt(st.nextToken());
            this.minBid = Integer.parseInt(st.nextToken());
            this.buyoutPrice = Integer.parseInt(st.nextToken());
            this.owner = st.nextToken();
            this.bidAmount = Integer.parseInt(st.nextToken());
            this.highBidder = (st.nextToken().equals("1"));
            this.timeLeft = Integer.parseInt(st.nextToken());
            this.lastSeen = Long.parseLong(st.nextToken());
            this.expiration = Long.parseLong(st.nextToken());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getAuctionId() {
        return auctionId;
    }

    public int getBuyoutPrice() {
        return buyoutPrice;
    }

    public int getCount() {
        return count;
    }

    public void setServerFaction(String serverFaction) {
        this.serverFaction = serverFaction;
    }

    public String getKey() {
        return serverFaction + ":" + auctionId;
    }
    
    public void write(Connection theDb) {
        String sqlText;
        Statement sql;

        try {
            sql = theDb.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Failed to create SQL statement");
            return;
        }

        Timestamp expirationStamp = new Timestamp(expiration * 1000);
        Timestamp lastSeenStamp = new Timestamp(lastSeen * 1000);

        sqlText = "insert into auctions (object_id, unique_id, count, min_bid, buyout_price, owner, bid_amount, "
                + "high_bidder, time_left, last_seen, expiration, server_faction, auction_id) values ("
                + "'" + objectId + "', "
                + uniqueId + ", "
                + count + ", "
                + minBid + ", "
                + buyoutPrice + ", "
                + "'" + owner + "', "
                + bidAmount + ", "
                + highBidder + ", "
                + timeLeft + ", "
                + "'" + lastSeenStamp.toString()  + "', "
                + "'" + expirationStamp.toString() + "', "
                + "'" + serverFaction + "', "
                + auctionId
                + ")";

        try {
            sql.executeUpdate(sqlText);
        } catch (SQLException e) {
            if (e.getMessage().startsWith("ERROR: duplicate key")) {
                // Ignore duplicate key errors
                if (AuctionExtract.DEBUG) {
                    System.out.println("Duplicate Key Error: uniqueId=" + uniqueId + ", serverFaction = '"
                            + serverFaction + "', "
                            + "objectId = '" + objectId + "'");
                }
            } else {
                e.printStackTrace();
                System.out.println("Update failed");
            }
        }

    }
}
